using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;

namespace ComputerShopE3.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/
        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();

        public ActionResult Index()
        {
            //var dssp = from sp in _db.SAN_PHAM
            //         join ha in _db.HINH_ANH
            //         on sp.id equals ha.SAN_PHAM.id
            //         select new ProductViewModel
            //         {
            //             id = ha.id,
            //             Diem = sp.Diem,
            //             Gia = sp.Gia,
            //             idKhuyenMai = sp.idKhuyenMai,
            //             TenLoaiSanPham = sp.LOAI_SAN_PHAM.TenLoaiSanPham,
            //             MauSac = ha.MauSac,
            //             MoTa = sp.MoTa,
            //             TenSanPham = sp.TenSanPham,
            //             Url = ha.Url
            //         };

            //List<SanPhams> sps = new List<SanPhams>();

            //SanPhams sp2 = null;

            //foreach (var item in db)
            //{
            //    sp2 = new SanPhams();
            //    sp2.sp = item.sp;
            //    sp2.ha = item.ha;

            //    sps.Add(sp2);
            //}

            //ViewData["SapPham_HinhAnh"] = dssp;

            foreach (var sp in _db.SAN_PHAM)
            {
                sp.LOAI_SAN_PHAMReference.Load();
                sp.NHA_PHAN_PHOIReference.Load();
                sp.NHA_SAN_XUATReference.Load();
                sp.HINH_ANH.Load();
            }

            return View(_db.SAN_PHAM.ToList());
        }

        //
        // GET: /Product/Details/5

        public ActionResult Details(int id)
        {
            var sp = _db.SAN_PHAM.Where((c) => c.id == id).FirstOrDefault();
            //ViewData["GOP_Y"] = _db.GOP_Y.Where((c) => c.SAN_PHAM.id == sp.id);

            //SAN_PHAM sp = (SAN_PHAM)ViewData["SAN_PHAM"];

            sp.LOAI_SAN_PHAMReference.Load();
            sp.NHA_PHAN_PHOIReference.Load();
            sp.NHA_SAN_XUATReference.Load();
            sp.HINH_ANH.Load();

            return View(sp);
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Product/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection form)
        {
            try
            {
                // TODO: Add insert logic here

                string id = form.Get("id");
                string TenSanPham = form.Get("TenSanPham");
                string MoTa = form.Get("MoTa");
                long Gia = long.Parse(form.Get("Gia"));
                short Diem = short.Parse(form.Get("Diem"));
                short SoLuongTon = short.Parse(form.Get("SoLuongTon"));
                string BaoHanh = form.Get("BaoHanh");
                DateTime NgayThemSP = DateTime.Parse(form.Get("NgayThemSP"));
                int LoaiLinhKien = int.Parse(form.Get("LoaiLinhKien"));
                int KM = int.Parse(form.Get("idKhuyenMai"));
                SAN_PHAM tt = new SAN_PHAM
                {
                    TenSanPham = TenSanPham,
                    MoTa = MoTa,
                    Gia = Gia,
                    Diem = Diem,
                    SoLuongTon = SoLuongTon,
                    BaoHanh = BaoHanh,
                    NgayThemSP = NgayThemSP,
                };
                var t = from nd in _db.KHUYEN_MAI
                        where nd.id == KM
                        select nd;
                //tt.KHUYEN_MAI = t.First();



                _db.SaveChanges();



                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Product/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Product/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult CompareProduct()
        {
            ViewData["NhaSanXuat"] = new SelectList(_db.NHA_SAN_XUAT.ToList(), "id", "TenNSX");
            ViewData["TenSanPham"] = new SelectList(_db.SAN_PHAM.ToList(), "id", "TenSanPham");
            return View(); 
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CompareProduct(FormCollection form)
        {
            ViewData["NhaSanXuat"] = new SelectList(_db.NHA_SAN_XUAT.ToList(), "id", "TenNSX");
            ViewData["TenSanPham"] = new SelectList(_db.SAN_PHAM.ToList(), "id", "TenSanPham");
            try
            {
                int idNSX1 = int.Parse(form.Get("idNSX1"));
                int idNSX2 = int.Parse(form.Get("idNSX1"));

                int id1 = int.Parse(form.Get("TenSanPham1"));
                int id2 = int.Parse(form.Get("TenSanPham2"));
                var rs1 = _db.SAN_PHAM.Where(sp => sp.id == id1).First();// && sp.NHA_SAN_XUAT.id == idNSX1);
                var rs2 = _db.SAN_PHAM.Where(sp => sp.id == id2).First();// && sp.NHA_SAN_XUAT.id == idNSX2);
                ViewData["SanPham1"] = rs1;
                ViewData["SanPham2"] = rs2;
            }
            catch (System.Exception e)
            {
            	
            }
            return View();
        }
        public ActionResult Search()
        {
            ViewData["NhaSanXuat"] = new SelectList(_db.NHA_SAN_XUAT.ToList(), "id", "TenNSX");
            ViewData["NhaPhanPhoi"] = new SelectList(_db.NHA_PHAN_PHOI.ToList(), "id", "TenNhaPhanPhoi");
            ViewData["LoaiSanPham"] = new SelectList(_db.LOAI_SAN_PHAM.ToList(), "id", "TenLoaiSanPham");
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Search(FormCollection form)
        {
            ViewData["NhaSanXuat"] = new SelectList(_db.NHA_SAN_XUAT.ToList(), "id", "TenNSX");
            ViewData["NhaPhanPhoi"] = new SelectList(_db.NHA_PHAN_PHOI.ToList(), "id", "TenNhaPhanPhoi");
            ViewData["LoaiSanPham"] = new SelectList(_db.LOAI_SAN_PHAM.ToList(), "id", "TenLoaiSanPham");

            try
            {
                string TenSanPham = form.Get("TenSanPham");
                int idNSX = int.Parse(form.Get("idNSX"));
                int idNhaPhanPhoi = int.Parse(form.Get("idNhaPhanPhoi"));
                int idLoaiSanPham = int.Parse(form.Get("idLoaiSanPham"));
                int giaMin = int.Parse(form.Get("idGiaMin"));
                int giaMax = int.Parse(form.Get("idGiaMax"));
                //String soSanh = form.Get("idSoSanh");

                var Sps = _db.SAN_PHAM.Where(sp => TenSanPham==null?true:sp.TenSanPham.Contains(TenSanPham) 
                                                    && (giaMin==-1?true:(sp.Gia >= giaMin))
                                                    && (giaMax==-1?true:(sp.Gia <= giaMax))
                                                    && (idNSX==-1?true:((int)sp.NHA_SAN_XUAT.id == idNSX))
                                                    && (idNhaPhanPhoi==-1?true:(int)sp.NHA_PHAN_PHOI.id == idNhaPhanPhoi)
                                                    && (idLoaiSanPham==-1?true:(int)sp.LOAI_SAN_PHAM.id == idLoaiSanPham));
                ViewData["KetQua"] = Sps.ToList();
                
                return View();
            }
            catch (Exception ex)
            {

                ViewData["error"] = ex.Message;
                return View();
            }
        }
    }
}
